﻿using ACC.Data.Entities.Common;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using ACC.Extensions.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ACC.Extensions.PCClient.UserControls
{
    /// <summary>
    /// Interaction logic for GridPicControl.xaml
    /// </summary>
    public partial class GridPicControl : UserControl
    {
        public GridPicControl()
        {
            InitializeComponent();
        }

        private Point _p;
        protected readonly string WORD = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        protected readonly string BACKCOLOR01 = "0xFF004080";
        protected readonly string BACKCOLOR02 = "0xFFCCE8CF";
        protected readonly string BACKCOLOR03 = "0xFF000000";
        protected readonly string BACKCOLOR04 = "0xFFFFFFFF";
        protected readonly string BACKCOLOR05 = "0xFF5B6568";
        protected readonly string BACKCOLOR06 = "0xFF757575";
        protected readonly string BACKCOLOR07 = "0xFF717171";

        public GridPicDetail GridPicInfo = new GridPicDetail();

        public delegate void MenuItemClickHandle();
        public event MenuItemClickHandle MenuItemClickEvent;

        private Point pRegion1LeftUp = new Point();
        private Point pRegion1RightDown = new Point();
        private Point pRegion2LeftUp = new Point();
        private Point pRegion2RightDown = new Point();

        private double _Region1CellWidth;
        private double _Region1CellHeight;
        private string _Region1FirstChar;
        private string _Region1FirstNum;
        private double _Region2CellWidth;
        private double _Region2CellHeight;
        private string _Region2FirstChar;
        private string _Region2FirstNum;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            double Height;
            double Width;

            Type t = GridPicInfo.GetType();
            PropertyInfo[] props = t.GetProperties();
            foreach (var item in props)
            {
                if (item.GetValue(GridPicInfo) == null)
                {
                    return;
                }
            }
            ClientHelper.LoadImageSource(GridPicInfo.FilePath, this.imgPic, out Height, out Width);
            this.cvsImgPic.Width = Width;
            this.cvsImgPic.Height = Height;

            pRegion1LeftUp.X = Convert.ToDouble((GridPicInfo.Region1LeftUpCoord.Split(','))[0]);
            pRegion1LeftUp.Y = Convert.ToDouble((GridPicInfo.Region1LeftUpCoord.Split(','))[1]);
            _Region1FirstChar = GridPicInfo.Region1LeftUpPosition.Substring(GridPicInfo.Region1LeftUpPosition.Length - 1,1).ToUpper();
            _Region1FirstNum = GridPicInfo.Region1LeftUpPosition.Substring(0, GridPicInfo.Region1LeftUpPosition.Length - 1);

            pRegion1RightDown.X = Convert.ToDouble((GridPicInfo.Region1RightDownCoord.Split(','))[0]);
            pRegion1RightDown.Y = Convert.ToDouble((GridPicInfo.Region1RightDownCoord.Split(','))[1]);
            string strChar = GridPicInfo.Region1RightDownPosition.Substring(GridPicInfo.Region1RightDownPosition.Length - 1, 1).ToUpper();
            string strNum = GridPicInfo.Region1RightDownPosition.Substring(0, GridPicInfo.Region1RightDownPosition.Length - 1);
            _Region1CellHeight = (pRegion1RightDown.Y - pRegion1LeftUp.Y) / (int.Parse(strNum) - int.Parse(_Region1FirstNum)+1);
            _Region1CellWidth = (pRegion1RightDown.X- pRegion1LeftUp.X) / (WORD.IndexOf(strChar) - WORD.IndexOf(_Region1FirstChar)+1);

            pRegion2LeftUp.X = Convert.ToDouble((GridPicInfo.Region2LeftUpCoord.Split(','))[0]);
            pRegion2LeftUp.Y = Convert.ToDouble((GridPicInfo.Region2LeftUpCoord.Split(','))[1]);
            _Region2FirstChar = GridPicInfo.Region2LeftUpPosition.Substring(GridPicInfo.Region2LeftUpPosition.Length - 1, 1).ToUpper();
            _Region2FirstNum = GridPicInfo.Region2LeftUpPosition.Substring(0, GridPicInfo.Region2LeftUpPosition.Length - 1);

            pRegion2RightDown.X = Convert.ToDouble((GridPicInfo.Region2RightDownCoord.Split(','))[0]);
            pRegion2RightDown.Y = Convert.ToDouble((GridPicInfo.Region2RightDownCoord.Split(','))[1]);
            strChar = GridPicInfo.Region2RightDownPosition.Substring(GridPicInfo.Region2RightDownPosition.Length - 1, 1).ToUpper();
            strNum = GridPicInfo.Region2RightDownPosition.Substring(0, GridPicInfo.Region2RightDownPosition.Length - 1);
            _Region2CellHeight = (pRegion2RightDown.Y - pRegion2LeftUp.Y) / (int.Parse(strNum) - int.Parse(_Region2FirstNum)+1);
            _Region2CellWidth = (pRegion2RightDown.X - pRegion2LeftUp.X) / (WORD.IndexOf(strChar) - WORD.IndexOf(_Region2FirstChar)+1);

            this.imgPic.ContextMenu = null;
        }

        public void AddMenuItem(List<ClientFailureWord> lstStatusWordMenu)
        {
            this.menuGridPic.Items.Clear();

            if (lstStatusWordMenu.Count == 1)
            {
                foreach (var itemBit in lstStatusWordMenu.FirstOrDefault().StatusBits)
                {
                    MenuItem mi = new MenuItem();
                    mi.Height = 50;
                    mi.FontSize = 25;
                    mi.FontWeight = FontWeights.Bold;
                    mi.VerticalAlignment = VerticalAlignment.Center;
                    mi.Header = itemBit.Description == null ? "" : itemBit.Description;
                    mi.Tag = itemBit.Bit == null ? 9999 : itemBit.Bit;
                    mi.Click += MenuItem_Click;
                    this.menuGridPic.Items.Add(mi);
                }
            }
            else
            {
                foreach (var item in lstStatusWordMenu)
                {
                    MenuItem myItem = new MenuItem();
                    myItem.Height = 50;
                    myItem.FontSize = 25;
                    myItem.FontWeight = FontWeights.Bold;
                    myItem.VerticalAlignment = VerticalAlignment.Center;
                    myItem.Header = item.Description;
                    if (item.StatusBits != null)
                    {
                        foreach (var itemBit in item.StatusBits)
                        {
                            MenuItem mi = new MenuItem();
                            mi.Height = 50;
                            mi.FontSize = 25;
                            mi.FontWeight = FontWeights.Bold;
                            mi.VerticalAlignment = VerticalAlignment.Center;
                            mi.Header = itemBit.Description == null ? "" : itemBit.Description;
                            mi.Tag = itemBit.Bit == null ? 9999 : itemBit.Bit;
                            mi.Click += MenuItem_Click;
                            myItem.Items.Add(mi);
                        }
                        this.menuGridPic.Items.Add(myItem);
                    }
                }
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            TextBox txtBox = new TextBox();
            txtBox.Background = new SolidColorBrush(Colors.Red);
            txtBox.IsReadOnly = true;

            double dCellWidth;
            double dCellHeight;
            Point pntRegion;
            int firstNum;
            string firstChar;
            if (PointInFences(_p,new Point[]{pRegion1LeftUp,pRegion1RightDown}))
            {
                dCellWidth = _Region1CellWidth;
                dCellHeight = _Region1CellHeight;
                pntRegion = pRegion1LeftUp;
                firstNum = int.Parse(_Region1FirstNum);
                firstChar = _Region1FirstChar;
            }
            else
            {
                dCellWidth = _Region2CellWidth;
                dCellHeight = _Region2CellHeight;
                pntRegion = pRegion2LeftUp;
                firstNum = int.Parse(_Region2FirstNum);
                firstChar = _Region2FirstChar;
            }

            int x = Math.Ceiling((_p.Y - pntRegion.Y) / dCellHeight).ToInt();
            int y = Math.Ceiling((_p.X - pntRegion.X) / dCellWidth).ToInt();
            string strPosition = string.Format("{0}{1}", x + firstNum, y > WORD.Length ? "?" : WORD.Substring(y+( WORD.IndexOf(firstChar) < 0 ? 0 : WORD.IndexOf(firstChar)), 1));

            if ((sender as MenuItem).Parent as MenuItem != null) //二级菜单
            {
                //等级|位置
                txtBox.Text = (sender as MenuItem).Header.ToString() + "|" + strPosition;
                //Bit位|原因
                txtBox.Tag = (sender as MenuItem).Tag + "|" + ((sender as MenuItem).Parent as MenuItem).Header;
            }
            else
            { 
                //等级|位置
                txtBox.Text = "|" + strPosition;
                //Bit位|原因
                txtBox.Tag = (sender as MenuItem).Tag + "|" + (sender as MenuItem).Header.ToString();
            }
            txtBox.FontSize = 15;
            txtBox.FontWeight =FontWeights.Bold;
            txtBox.VerticalContentAlignment = VerticalAlignment.Center;
            txtBox.Width = dCellWidth;
            txtBox.Height = dCellHeight;
            txtBox.MinHeight = dCellHeight;
            txtBox.SetValue(Canvas.LeftProperty, (double)(y-1) * dCellWidth+pntRegion.X);
            txtBox.SetValue(Canvas.TopProperty, (double)(x-1) * dCellHeight+pntRegion.Y);
            txtBox.MouseDoubleClick += TextMouseDoubleClick;

            this.cvsImgPic.Children.Add(txtBox);

            if (MenuItemClickEvent != null)
            {
                MenuItemClickEvent();
            }
        }

        private void TextMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.cvsImgPic.Children.Remove(sender as TextBox);
            if (MenuItemClickEvent != null)
            {
                MenuItemClickEvent();
            }
        }
        //禁用touch弹菜单
        private void cvsImgPic_TouchDown(object sender, TouchEventArgs e)
        {
            this.imgPic.ContextMenu = null;
            this.menuGridPic.IsOpen = false;
        }
        private void cvsImgPic_TouchUp(object sender, TouchEventArgs e)
        {
            this.imgPic.ContextMenu = null;
            this.menuGridPic.IsOpen = false;
        }
        private void cvsImgPic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.imgPic.ContextMenu = null;
            this.menuGridPic.IsOpen = false;
        }

        private void cvsImgPic_MouseUp(object sender, MouseButtonEventArgs e)
        {
            _p = e.GetPosition((IInputElement)sender);
            if (e.ChangedButton == MouseButton.Right)
            {
                this.imgPic.ContextMenu = null;
                this.menuGridPic.IsOpen = false;
              //  return;
            }

            if (!PointInFences(_p, new Point[] { pRegion1LeftUp, pRegion1RightDown })
                && !PointInFences(_p, new Point[] { pRegion2LeftUp, pRegion2RightDown }))
            {
                return;
            }
            //相对屏幕位置
            //System.Drawing.Point point = System.Windows.Forms.Cursor.Position;
            Point point = e.MouseDevice.GetPosition(this);  //获取鼠标相对位置
            System.Drawing.Color color = ColorPickerHelper.GetColor((int)(point.X), (int)(point.Y));
            string strColor = "0x" + color.ToArgb().ToString("X");
            if (strColor != BACKCOLOR01
                && strColor != BACKCOLOR02
                && strColor != BACKCOLOR03
                && strColor != BACKCOLOR04
                //&& strColor != BACKCOLOR05
                //&& strColor != BACKCOLOR06
                && strColor != BACKCOLOR07)
            {
                this.menuGridPic.PlacementTarget = this.imgPic;
                this.menuGridPic.IsOpen = true;
            }
        }

        private bool PointInFences(Point pnt1, Point[] fencePnts)
        {
            if (fencePnts.Count() < 2)
            {
                return false;
            }
            if (pnt1.X < fencePnts[0].X || pnt1.X > fencePnts[1].X)
            {
                return false;
            }
            if (pnt1.Y < fencePnts[0].Y || pnt1.Y > fencePnts[1].Y)
            {
                return false;
            }
            return true;
        }
    }
}
